
package utils;

/**
 *
 * @author trong_000
 */
import java.text.NumberFormat;

public class Matrix {

  private double[] data;

  private int row;
  private int col;
/**
 * Constructor
 * @param r : số hàng
 * @param c : số cột
 */
  public Matrix(int r, int c) {
    this.data = new double[r * c];

    this.row = r;
    this.col = c;
  }

  public double[] getData() {
    return data;
  }

  public void setData(double[] data) {
    assert (data.length % col == /* */0);
    assert (data.length / col == /**/row);

    this.data = data;
  }

  
  public int getRow() {
    return row;
  }

  public int getCol() {
    return col;
  }

  public void set(int r, int c, double v) {
    assert (r < row && c < col); //nếu có lỗi thì dừng

    data[col * r + c] = v;
  }

  public double get(int r, int c) {
    assert (r < row && c < col);

    return data[col * r + c];
  }


  @Override
  public String toString() {
    StringBuffer ret = new StringBuffer();

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    nf.setMinimumFractionDigits(4);
    nf.setGroupingUsed(false);

    for (int f = 0; f < row; f++) {
      for (int c = 0; c < col; c++) {
        ret.append(nf.format(get(f, c)));
        ret.append(" ");
      }

      ret.append("\n");
    }

    return ret.toString();
  }
    
}